﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TruckWatch.Cloud.Models;

namespace TruckWatch.Cloud.BusinessLogic.Util
{
    public static class LatLongUtil
    {

        /// <summary>
        /// Calculates the distance between two points of latitude and longitude.
        /// Great Link - http://www.movable-type.co.uk/scripts/latlong.html
        /// </summary>
        /// <param name="coordinate1">First coordinate.</param>
        /// <param name="coordinate2">Second coordinate.</param>
        /// <param name="unitsOfLength">Sets the return value unit of length.</param>
        public static Double DistanceInMiles(GeoPoint coordinate1, GeoPoint coordinate2)
        {

            var theta = coordinate1.Longitude - coordinate2.Longitude;
            var distance = Math.Sin(coordinate1.Latitude.ToRadian()) * Math.Sin(coordinate2.Latitude.ToRadian()) +
                                Math.Cos(coordinate1.Latitude.ToRadian()) * Math.Cos(coordinate2.Latitude.ToRadian()) *
                                Math.Cos(theta.ToRadian());

            distance = Math.Acos(distance);
            distance = distance.ToDegree();
            distance = distance * 60 * 1.1515;

            return (distance);

        }



    }
}
